/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MilestoneInfo.java
 *
 * Created on Jul 4, 2011, 2:43:13 AM
 */

package javadatabasegui;

import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author PM
 */
public class MilestoneInfo extends javax.swing.JFrame {

    String userEmail;
    int projectID;
    Connection con;
    String msName;
    boolean isnew = false;
    /** Creates new form MilestoneInfo */
    public MilestoneInfo(String uEmail, int pID, Connection conn, String MSname) {
        userEmail = uEmail;
        projectID = pID;
        con = conn;
        msName = MSname;
        initComponents();
        displayTasksTable();

    }
    
    public MilestoneInfo(String uEmail, int pID, Connection conn, String MSname, boolean newM) {
        userEmail = uEmail;
        projectID = pID;
        con = conn;
        msName = MSname;
        isnew = newM;
        initComponents();
        displayTasksTable();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jTextField2 = new javax.swing.JTextField();
        checkbox1 = new java.awt.Checkbox();
        jPanel2 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel4 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 20));
        jLabel1.setText("Milestone Info");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Project");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Name");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("Description");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel5.setText("Due Date");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel6.setText("Complete?");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel7.setText(getProjectName());

        jTextField1.setText(msName);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setText(getMilestoneDesc());
        jScrollPane1.setViewportView(jTextArea1);

        jTextField2.setText(getDueDate());

        checkbox1.setState(getStatus());
        checkbox1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                checkboxMouseClicked(evt);
            }
        });

        jPanel2.setBackground(new java.awt.Color(219, 238, 244));

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel8.setText("Tasks");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel9.setText("Status");

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel10.setText("Name");

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel11.setText("Assigned To");

        jButton1.setText("New Task");
        jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                NewTaskMouseClicked(evt);
            }
        });

        jScrollPane2.setBorder(null);

        jPanel4.setBackground(new java.awt.Color(219, 238, 244));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 626, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 183, Short.MAX_VALUE)
        );

        jScrollPane2.setViewportView(jPanel4);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 484, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(107, 107, 107)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 146, Short.MAX_VALUE)
                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(90, 90, 90))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabel12.setBackground(new java.awt.Color(255, 255, 255));
        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel12.setForeground(new java.awt.Color(0, 0, 255));
        jLabel12.setText("<html> <u>Project Milestones</u></html>");
        jLabel12.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jLabel12.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ProjectMilestonesMouseClicked(evt);
            }
        });

        jPanel3.setBackground(new java.awt.Color(230, 174, 39));

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jButton2.setText("Save");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                SaveButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(552, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 356, Short.MAX_VALUE)
                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(checkbox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE)
                            .addComponent(jScrollPane1)
                            .addComponent(jTextField1)))
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(checkbox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ProjectMilestonesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ProjectMilestonesMouseClicked
        new ProjectMilestones(projectID,userEmail, con).setVisible(true);
            this.dispose();
    //Opens the Project Milestones tab if clicked
    }//GEN-LAST:event_ProjectMilestonesMouseClicked

    private void NewTaskMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_NewTaskMouseClicked
        if(isnew == false){
            new TaskInfo(userEmail, projectID, "New Task Name", msName, con).setVisible(true);
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(null, "Warning: Must save new milestone before creating a new task.");
        }
    //Opens blank Task Info tab if clicked
    }//GEN-LAST:event_NewTaskMouseClicked

    private void SaveButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SaveButtonMouseClicked
        String mTitle = jTextField1.getText();
        String mDesc = jTextArea1.getText();
        String mDueDate = jTextField2.getText();
        int mStatus = 1;
        boolean cst = checkbox1.getState();
        if (cst == true)
            mStatus = 0;
        
        if (mTitle.equals("") || mDesc.equals("") || mDueDate.equals("")) {
            JOptionPane.showMessageDialog(null, "Cannot save with empty fields.");
        }else{
            //Checking date format
            if (checkDate(mDueDate) == false){
                System.out.println("Due date not valid");
            }else{
                Date Dt = null;
                DateFormat fml = new SimpleDateFormat("yyyy-mm-dd");
                try {
                    Dt = (Date)fml.parse(mDueDate);
                }catch (ParseException e){}
            //*
                if(isnew == true){
                    try{
                        //Add the new milestone to the database
                        Statement stmt = con.createStatement();

                        stmt.execute("INSERT INTO `cs4400_Group_14`.`Milestones` (`PID`, `M_Name`, `M_Desc`, `Due_Date`, `Status`) VALUES ('"
                                + projectID +"', '"+ mTitle +"', '"+ mDesc +"', '"+ mDueDate +"', '"+ mStatus +"')");
                    }catch(SQLException e){
                        System.err.println("Error trying to add the new milestone");
                        System.err.println(e.getMessage());
                    }
                }else{
                    System.out.println("not new");
                        try{
                            //Update the new project to the database
                            Statement stmt = con.createStatement();
                            stmt.execute("Update `cs4400_Group_14`.`Milestones` SET "
                                    + "`PID` = '" + projectID + "', "
                                    + "`M_Name` = '" + mTitle + "', "
                                    + "`M_Desc` = '" + mDesc + "', "
                                    + "`Due_Date` = '" + mDueDate + "', "
                                    + "`Status` = '" + mStatus + "'"
                                    + "WHERE `PID` = '" + projectID +"' AND `M_Name` = '" + msName +"'");
                        }catch(SQLException e){
                            System.err.println("Error trying to update the milestone");
                            System.err.println(e.getMessage());
                    }
                }

                }
            new MilestoneInfo(userEmail, projectID, con, mTitle).setVisible(true);
                    this.dispose();        
                }    
        
    }//GEN-LAST:event_SaveButtonMouseClicked

    private void checkboxMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_checkboxMouseClicked
        boolean opentask = false;
        //if checkbox status = true
        if (checkbox1.getState() == true) {
            // check to see if any task is still active
            try{
                Statement stmt = con.createStatement();
                ResultSet rs = null;
                if (stmt.execute("SELECT Status from `cs4400_Group_14`.`Tasks` WHERE `PID` = \"" + projectID + "\" AND `M_Name` = \"" + msName + "\" AND `Status` = 1")){
                    rs = stmt.getResultSet();
                }
                rs.next();
                opentask = rs.getBoolean("Status");
                System.out.println("Status" + opentask);
            }catch(SQLException e){
                System.err.println("Error in Task Status");
                System.err.println(e.getMessage());
            }
                // if yes, return message saying all tasks must be closed and return checkbox to false
            if (opentask == true)
                JOptionPane.showMessageDialog(null, "Cannot deactivate Milestone with active Tasks.");
                checkbox1.setState(false) ; 
                // if no, checkbox stays true
        }
    }//GEN-LAST:event_checkboxMouseClicked

    private boolean checkDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date Dt = null;
        try {
            Dt = sdf.parse(str);
        }catch (ParseException e){ 
        return false;
        }
        System.out.println("Dt= " + Dt);
        if (!sdf.format(Dt).equals(str)) {
            JOptionPane.showMessageDialog(null, "Invalid Date Format.");
            return false;
        }else{
            Date lastdate = null;
            try{
                Statement stmt = con.createStatement();
                ResultSet rs = null;
                if (stmt.execute("SELECT MAX(Due_Date) from `cs4400_Group_14`.`Tasks` WHERE `PID` = \"" + projectID + "\" AND `M_Name` = \"" + msName + "\"")){
                    rs = stmt.getResultSet();
                }
                rs.next();
                lastdate = rs.getDate("MAX(Due_Date)");
                System.out.println("lastdate = " + lastdate);
            }catch(SQLException e){
                System.err.println("Error in Task DD calc");
                System.err.println(e.getMessage());
            }
            //need this if statement to see if Dt is before lastdate
            if (lastdate == null){
                return true;
            }else{
                if (Dt.before(lastdate) && Dt != lastdate) {
                    String ld = "";
                    JOptionPane.showMessageDialog(null, "Date before last Task Due Date.");
                    SimpleDateFormat dateform = new SimpleDateFormat("yyyy-MM-dd");
                    ld = dateform.format(lastdate);
                    try{
                        Statement stmt = con.createStatement();
                        ResultSet rs = null;
                        if (stmt.execute("UPDATE `cs4400_Group_14`.`Milestones` SET `Due_Date`= \"" + lastdate + "\" WHERE `PID`= \"" + projectID + "\" and`M_Name`= \"" + msName + "\"")){
                        }
                    }catch(SQLException e){
                        System.err.println("Error in Task DD calc");
                        System.err.println(e.getMessage());
                    }
                    return false;
                }else{
                    return true;
                }
            }
        }
    }
    
    private String getProjectName(){
        String P_Name = "Project";
        try{
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            if (stmt.execute("SELECT P_Name FROM cs4400_Group_14.Project WHERE PID=\"" + projectID + "\"")){
                rs = stmt.getResultSet();
            }
            rs.next();
            P_Name = rs.getString("P_Name");            
        }catch(SQLException e){
            System.err.println(e.getMessage());
            }
        return P_Name;
    }

    private String getMilestoneDesc(){
        String M_Desc = "Description";
        try{
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            if (stmt.execute("SELECT M_Desc FROM cs4400_Group_14.Milestones WHERE M_Name = \"" + msName + "\" AND PID = \"" + projectID + "\"")){
                rs = stmt.getResultSet();
            }
            rs.next();
            M_Desc = rs.getString("M_Desc");
        }catch(SQLException e){
            System.err.println(e.getMessage());
            }
        return M_Desc;
    }
    
    private String getDueDate(){
        Date DT = null;
        String DD = "YYYY-MM-DD";
        if (isnew == false) {
            try{
                Statement stmt = con.createStatement();
                ResultSet rs = null;
                if (stmt.execute("SELECT Due_Date FROM cs4400_Group_14.Milestones WHERE M_Name = \"" + msName + "\" AND PID = \"" + projectID + "\"")){
                    rs = stmt.getResultSet();
                }
                rs.next();
                DT = rs.getDate("Due_Date");
                DD = DT.toString();
            }catch(SQLException e){
                System.err.println(e.getMessage());
                }
        }
        return DD;
    }
    
    private Boolean getStatus(){
        Boolean Status = false;
        try{
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            if (stmt.execute("SELECT Status FROM cs4400_Group_14.Milestones WHERE M_Name = \"" + msName + "\" AND PID = \"" + projectID + "\"")){
                rs = stmt.getResultSet();
            }
            rs.next();
            if (rs.getInt("Status") == 0)
                Status = true;
        }catch(SQLException e){
            System.err.println(e.getMessage());
            }            
        return Status;
    }
    
    private void displayTasksTable(){
        int tblEntries = 0;
        
        // Getting the needed size of the box
        try{
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            if (stmt.execute("SELECT COUNT(T_Name) from `cs4400_Group_14`.`Tasks` WHERE `PID` = \"" + projectID + "\" AND `M_Name` = \"" + msName + "\"")){
                rs = stmt.getResultSet();
            }
            rs.next();
            tblEntries = rs.getInt("COUNT(T_Name)");
        }catch(SQLException e){
            System.err.println("Error in Task Count");
            System.err.println(e.getMessage());
            }
        
        //Sets the size of the panel to the correct height for the number of entries.
        jPanel4.setPreferredSize(new java.awt.Dimension(585, tblEntries*22));
        
        //Creats an array type to hold the labels (labels are each line in the panel)
        javax.swing.JLabel[] tstLabel = new javax.swing.JLabel[tblEntries];
        
        //Fills the array with new labels.
        for(int i=0; i<tstLabel.length; i++){
            tstLabel[i] = new javax.swing.JLabel();
        }
        
        //This try catch block will get the data from the database for each line
        //that we want to show. It will put that data into the labels of the array
        //created above.
        try{
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            Date currentDate = getDate();
            if (stmt.execute("SELECT Tasks.Status, Tasks.T_Name, Tasks.Due_Date, Users.F_Name, Users.L_Name FROM `cs4400_Group_14`.`Tasks`, `cs4400_Group_14`.`Users` WHERE Tasks.PID = \"" + projectID + "\" AND Tasks.M_Name = \"" + msName + "\" AND Tasks.U_Email = Users.Email GROUP BY T_Name ORDER BY Tasks.Status ASC, Tasks.Due_Date ASC")){
                rs = stmt.getResultSet();
            }
            
            // START formatting of table
            for(int i=0; rs.next();i++){
                if( currentDate.after(rs.getDate("Tasks.Due_Date")) && rs.getInt("Tasks.Status") == 1) {
                    tstLabel[i].setText("<html> <body> <table width=\"585\"> <tr> <td width=\"30%\"> <span style=\"color:red\">" + "Overdue since " + rs.getDate("Tasks.Due_Date") + "</td>"
                        + "<td width=\"40%\"> <u><span style=\"color:blue\">" + rs.getString("T_Name") + "</u></td>"
                        + "<td width=\"20%\">" + rs.getString("F_Name") + " " + rs.getString("L_Name") + "</td> </tr> </table> </body> </html>");
                    tstLabel[i].setName(rs.getString("T_Name"));
                }
                else {
                    if( currentDate.before(rs.getDate("Tasks.Due_Date")) && rs.getInt("Tasks.Status") == 1) {
                    tstLabel[i].setText("<html> <body> <table width=\"585\"> <tr> <td width=\"30%\">" + "Active until " + rs.getDate("Tasks.Due_Date") + "</td>"
                        + "<td width=\"40%\"> <u><span style=\"color:blue\">" + rs.getString("T_Name") + "</u></td>"
                        + "<td width=\"20%\">" + rs.getString("F_Name") + " " + rs.getString("L_Name") + "</td> </tr> </table> </body> </html>");
                    tstLabel[i].setName(rs.getString("T_Name"));
                }
                    else {
                        tstLabel[i].setText("<html> <body> <table width=\"585\"> <tr> <td width=\"30%\">" + "Complete" + "</td>"
                        + "<td width=\"40%\"> <u><span style=\"color:blue\">" + rs.getString("T_Name") + "</u></td>"
                        + "<td width=\"20%\">"  + rs.getString("F_Name") + " " + rs.getString("L_Name") + "</td> </tr> </table> </body> </html>");
                        tstLabel[i].setName(rs.getString("T_Name"));
                    }
                }
            }
        }catch(SQLException e){
            System.err.println("Error in displaying tasks");
            System.err.println(e.getMessage());
            }

        //this is much easier to display the components in the panel
        for(int i=0; i<tstLabel.length; i++){
            tstLabel[i].setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
            tstLabel[i].addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                taskClicked(evt);
            }});
        }

        jPanel4.setLayout(new GridBagLayout());
        GridBagConstraints cnst = new GridBagConstraints();
        for(int i=0; i<tstLabel.length; i++){
            tstLabel[i].setFont(new Font(tstLabel[i].getText(),4,12));
            cnst.gridx = 0;
            cnst.gridy = i;
            jPanel4.add(tstLabel[i],cnst);
        }
        cnst.gridy = tstLabel.length;
        cnst.gridx = 0;
        cnst.weighty = tblEntries * 15;
        jPanel4.add(new javax.swing.JLabel(), cnst);
    }
    
    private Date getDate(){
        Calendar cal = Calendar.getInstance();
        return cal.getTime();        
    }
    
    private void taskClicked(java.awt.event.MouseEvent evt ) {
        Component cmp = evt.getComponent();
        String str = cmp.getName();
        new TaskInfo(userEmail, projectID, str, msName, con).setVisible(true);
            this.dispose();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Checkbox checkbox1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

}
